package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.template.TbTypeTemplate;
import com.dk.service.template.TemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@Controller
@RequestMapping("template")
public class TypeTemplateController {

    private final Logger logger = LoggerFactory.getLogger(TypeTemplateController.class);

    @Reference
    private TemplateService templateService;

    @RequestMapping("toTypeTemplate")
    public String toTypeTemplate() {
        return "admin/type_template";
    }

    @RequestMapping("listTemplate")
    @ResponseBody
    public Result listTemplate() {
        try {
            List<TbTypeTemplate> list = templateService.listTemplate();
            return Result.response(ResultEnum.OK).setData(list);
        } catch (Exception e) {
            logger.error("查询模板列表错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("addTemplate")
    @ResponseBody
    public Result addTemplate (TbTypeTemplate template) {
        try {
            int i = templateService.insertTemplate(template);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("模型添加错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("templateById/{id}")
    @ResponseBody
    public Result templateById(@PathVariable("id") Long id) {
        try {
            TbTypeTemplate template = templateService.templateById(id);
            return Result.response(ResultEnum.OK).setData(template);
        } catch (Exception e) {
            logger.error("查询template错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("updateTemplate")
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate template) {
        try {
            int i = templateService.updateTemplate(template);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("模板修改错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("deleteTemplateByIds")
    @ResponseBody
    public Result deleteBrandByIds(@RequestParam("ids[]")Long[] ids) {
        try {
            int i = templateService.deleteTemplateByIds(ids);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("品牌删除错误:",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
